package com.zhangbaiyan.test2;

import java.io.IOException;
import java.net.*;

/**
 * @author zhangbaiyan
 * @date 2024-09-25 21:41
 * @description:
 * @version:
 */
public class send {
    public static void main(String[] args) throws IOException {
        //发送数据
        
        //1.创建DatagramSocket对象(快递公司)
        //细节:
        //绑定端口,以后我们就是通过这个端口往外发送
        //空参:所有可用的端口随机一个进行使用
        //有参:指定端口号进行绑定
        DatagramSocket ds = new DatagramSocket();

        //2.打包数据
        String str = "hello world";
        byte[] bytes = str.getBytes();
        InetAddress river = InetAddress.getByName("river");
        int port = 8080;

        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, river, port);

        //3.发送数据
        ds.send(dp);

        //释放数据
        ds.close();

    }
}
